package leetcode.D700.T633;

class Solution {
    // 双指针解法
    /*public boolean judgeSquareSum(int c) {
        int right = (int)Math.sqrt(c), left = 0;
        while(left <= right) {
            int sum = left*left + right*right;
            if(sum == c) {
                return true;
            } else if(sum > c || sum < 0) {
                right--;
            } else {
                left++;
            }
        }
        return false;
    }*/

    // 二刷
    /*public boolean judgeSquareSum(int c) {
        int left = 0, right = (int)Math.sqrt(c);
        while(left <= right) {
            int sum = left*left + right*right;
            if(sum == c) {
                return true;
            } else if (sum < 0 || sum > c) {
                right--;
            } else {
                left++;
            }
        }
        return false;
    }*/

    // 三刷
    public boolean judgeSquareSum(int c) {
        int j = (int) Math.sqrt(c), i = 0;
        while(i < j) {
            int sum = i * i + j * j;
            if (sum == c) {
                return true;
            } else if (sum < 0 || sum > c) {
                j--;
            } else {
                i++;
            }
        }
        return false;
    }
}
